#include "WriteFile.hpp"

namespace ge {

//----------------------------------------------------------------------------//
// WriteFile
//----------------------------------------------------------------------------//

//----------------------------------------------------------------------------//
WriteFile::WriteFile( void ) : m_handle( 0 )
{
}
//----------------------------------------------------------------------------//
WriteFile::~WriteFile( void )
{
     if( m_handle ) fclose( (FILE*)m_handle );
}
//----------------------------------------------------------------------------//
uint WriteFile::Write( const void* _src, uint _size )
{
     uint _writed = fwrite( _src, 1, _size, (FILE*)m_handle );
     m_pos += _writed;
     return _writed;
}
//----------------------------------------------------------------------------//
uint WriteFile::WriteTo( uint _offset, const void* _src, uint _size )
{
     if( _offset != m_pos ) fseek( (FILE*)m_handle, _offset, SEEK_SET );
     uint _writed = fwrite( _src, 1, _size, (FILE*)m_handle );
     if( m_pos != _offset + _writed ) fseek( (FILE*)m_handle, m_pos, SEEK_SET );
     return _writed;
}
//----------------------------------------------------------------------------//
void WriteFile::Flush( void )
{
     fflush( (FILE*)m_handle );
}
//----------------------------------------------------------------------------//
void WriteFile::ToEnd( void )
{
     fseek( (FILE*)m_handle, 0, SEEK_END );
     m_pos = ftell( (FILE*)m_handle );
}
//----------------------------------------------------------------------------//
void WriteFile::_Rewrite( const String& _path )
{
     assert( m_handle == 0 );
                                                                                                     
     m_handle = (void*)fopen( m_name = _path, "w+b" );
     geAssertT( IOError, m_handle != 0, "WriteFile - Could not open file \"%s\"", m_name.Data() );
}
//----------------------------------------------------------------------------//
void WriteFile::_Append( const String& _path )
{
     assert( m_handle == 0 );
                                                                                                     
     m_handle = (void*)fopen( m_name = _path, "a+b" );
     geAssertT( IOError, m_handle != 0, "WriteFile: Could not open file \"%s\"", m_name.Data() );
}
//----------------------------------------------------------------------------//
void WriteFile::_ApplyPos( void )
{
     fseek( (FILE*)m_handle, m_pos, SEEK_SET );
}
//----------------------------------------------------------------------------//

//----------------------------------------------------------------------------//
//
//----------------------------------------------------------------------------//

} // namespace ge
